package com.wz.vavr;

import io.vavr.API;
import io.vavr.control.Either;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.function.BinaryOperator;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static io.vavr.API.*;
import static io.vavr.Patterns.$Left;
import static io.vavr.Patterns.$Right;
import static junit.framework.Assert.assertEquals;

/**
 * Created by Administrator on 2020/5/30/030.
 */
public class MatchTest {

    public static <_1, _2, T, E, F> void testWenHao(Bar<_1, _2> b) {

    }


    static class Bar<T, R> {

        private T t;

        private R r;

        public void setT(T t) {
            this.t = t;
        }

        public T getT() {
            return (T) t;
        }

        public void setR(R r) {
            this.r = r;
        }

        public R getR() {
            return (R) r;
        }
    }

    static class Bar1<T> extends Bar {

        @Override
        public void setT(Object o) {
            super.setT(o);
        }

        @Override
        public Object getT() {
            return super.getT();
        }
    }

    static class Bar2<T, R> {

    }

    public static void testFlatMap(){
        List<Integer> together = Stream.of(Arrays.asList(1, 3), Arrays.asList(2, 4))
                                       .flatMap(number -> number.stream())
                                       .sorted(Comparator.comparing(integer -> integer))
                                       .collect(Collectors.toList());
        System.out.println(together);
        assertEquals(Arrays.asList(1, 2, 3, 4), together);
    }

    public static void testReduce(){
        int count = Stream.of(1, 2, 3).reduce( 0, (acc, element) -> acc + element);
        System.out.println(count);
    }


    public static void testEither(){
        Either<Integer, String> moneyOrBook = Either.right("1");
        //有匹配值
        String match_1 = Match(moneyOrBook).of(
                Case($Right($("")), "a"),//Case(Pattern,Function) $Right(Pattern<_1, ?> p1)->Pattern1<Right<L, R>, _1> $(T prototype)->Pattern0<T>
                //Case(Pattern,Function) $Right(Pattern<_1, ?> p1)->Pattern1<Right<L, R>, _1> $(String prototype)->Pattern0<String>
                Case($Left($(2)), "b"),
                Case($(), "c")
        );

        String match_2 = Match(moneyOrBook).of(
                Case($Right($()), "a"),
                Case($Left($()), "b")
        );
        System.out.println("the final value is " + match_1);
        System.out.println("the final value is " + match_2);
    }

    public static void main(String[] args) {

        testEither();
//        Bar1<String> b = new Bar1<String>();
//
//        testWenHao(b);
//
//        int bar = 0;
//        Match(bar).of(
//                Case($(1), o -> run(() -> System.out.println(""))),//Case(Pattern,Function) $Right(Pattern<_1, ?> p1)->Pattern1<Right<L, R>, _1> $(T prototype)->Pattern0<T>
//                Case($(), "2222")
//        );
//
//        BinaryOperator<Integer> bi = BinaryOperator.minBy(Comparator.naturalOrder());
//        System.out.println(bi.apply(2, 3));
//
//
//        List<String> collected1 = Stream.of(" a", "b", "c").collect(Collectors.toList());
//        assertEquals(Arrays.asList(" a", "b", "c"), collected1);
//
//        List< String> collected2 = Stream.of(" a", "b", "hello")
//                .map(string -> {return string.toUpperCase();})
//                .collect(Collectors.toList());
//
//
//        testFlatMap();
//        testReduce();
    }


}